package org.example;
import java.io.*;
//工具类
public class CommonUtil extends HttpServlet{
    private static  final byte CR='\r';
    private static final byte LF='\n';
    //html代码
    public static String htmlContent="";
    //java代码
    public static String javaContent="";
    public void commonMethod(MyHttpRequest req, MyHttpResponse resp) throws IOException {
        //获取请求uri
        String uri = req.getUri();
        File file = new File(HttpServer.WEB_ROOT,uri);
        BufferedReader reader = new BufferedReader(new FileReader(file));
        //解析jsp文件
        String line;
        while ((line = reader.readLine()) != null) {
            //判断是否以<%@ 开头
            if(line.startsWith("<%@")){
                //获取第一个等号和等号后面空格的位置
                int index1 = line.indexOf("=");
                int index2=line.indexOf(" ",index1);
                //设置响应的类型
                resp.addHeader("Content-Type",line.substring(index1+2,index2-1));
            }else{
                if(line.startsWith("<%")){
                    //java代码直接去除首尾<%%>
                    int index1 = line.indexOf("%");
                    int index2=line.indexOf("%",index1+1);
                    line=line.substring(index1+1,index2);
                    line+="\r\n";
                    javaContent+=line;
                }else{
                    //html代码
                    line+="\r\n";
                    htmlContent=htmlContent+line;
                }
            }
        }
        reader.close();
    }
}
